<?php
/**
 * panx 预设素材表
 *
 * Author: Zeon
 * Date: 2018/4/2
 * Created by Panxsoft.
 */
namespace App\Models\Panx;

use App\Models\Concerns\ScopeFilterable;
use App\Services\Utils\Bosable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * Class PanxAsset
 *
 * @method static self filterOnShelf(bool $on_shelf = true)
 *
 * @mixin Builder
 */
class PanxAsset extends Model
{
    use Bosable,
        ScopeFilterable;

    protected $table = 'panx_assets';

    protected $guarded = [];

    protected $casts = [
        'type_id'      => 'int',
        'is_deleted'   => 'int',
        'images'       => 'array',
        'texture_imgs' => 'array',
        'alpha_array'  => 'array',
    ];

    /**
     * @used-by filterOnShelf
     *
     * @param Builder $builder
     * @param bool $on_shelf
     *
     * @return Builder
     */
    public function scopeFilterOnShelf(Builder $builder, bool $on_shelf = true)
    {
        return $builder->where('is_deleted', ! $on_shelf);
    }

    /**
     * @return array
     */
    public function getImagesAttribute(): array
    {
        $img_keys = $this->attributes['images'];
        if (count($img_keys)) {
            return array_map([$this, 'getStaticUrl'], $img_keys);
        }

        return [];
    }
}
